using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dapper.Database.Attributes;
using SqlSugar;

namespace Shared.Contracts
{
    /// <summary>
    /// 可持久化到数据库的数据模型基类
    /// </summary>
    [Serializable]
    [JsonObject(MemberSerialization.OptOut)]
    public abstract class EntityBase
    {
        /// <summary>
        /// 是否需要记录属性变更,默认:true
        /// </summary>
        [Ignore]
        [JsonIgnore]
        [SugarColumn(IsIgnore = true)]
        public bool NeedRecordPropertyChange { get; set; } = true;
        /// <summary>
        /// 
        /// </summary>
        protected EntityBase()
        {
            NeedRecordPropertyChange = true;
            //IsDeleted = false;
            //CreatedTime = DateTime.Now;
        }
        /// <summary>
        /// 创建新的主键
        /// </summary>
        /// <returns></returns>
        [JsonIgnore]
        [Ignore]
        [SugarColumn(IsIgnore = true)]
        public string NewKey
        {
            get
            {
                return Guid.NewGuid().ToString().ToUpper();
            }
        }
        /// <summary>
        /// 获取当前时间
        /// </summary>
        /// <returns></returns>
        [JsonIgnore]
        [Ignore]
        [SugarColumn(IsIgnore = true)]
        public DateTime NowSystemTime
        {
            get { return DateTime.Now; }
        }
        /// <summary>
        /// 主键改变
        /// </summary>
        [JsonIgnore]
        private Dictionary<string, object> _PKPropertyChanges = new Dictionary<string, object>();
        /// <summary>
        /// 栏位值改变
        /// </summary>
        [JsonIgnore]
        private List<string> _PropertyChanges = new List<string>();
        /// <summary>
        /// 修改的主键栏位
        /// </summary>
        [JsonIgnore]
        [Ignore]
        [SugarColumn(IsIgnore = true)]
        public Dictionary<string, object> PKPropertyChanges
        {
            get { return _PKPropertyChanges; }
        }
        /// <summary>
        /// 修改的栏位
        /// </summary>
        
        [JsonIgnore]
        [Ignore]
        [SugarColumn(IsIgnore = true)]
        public List<string> PropertyChanges
        {
            get { return _PropertyChanges; }
        }
        /// <summary>
        /// 添加主键修改项
        /// </summary>
        /// <param name="propertyName">属性名称</param>
        protected void AddPKChange(string propertyName, dynamic oldValue, dynamic newValue)
        {
            if (!NeedRecordPropertyChange)
            {
                return;
            }
            object v = oldValue;
            if (null == oldValue || string.IsNullOrEmpty(Convert.ToString(oldValue)))
            {   //如果旧的值不存在,则不记录
                v = newValue;
            }
            if (!_PKPropertyChanges.Keys.Contains(propertyName))
            {
                _PKPropertyChanges.Add(propertyName, v);
            }
            else
            {
                _PKPropertyChanges[propertyName] = v;
            }
        }
        /// <summary>
        /// 添加修改项
        /// </summary>
        /// <param name="propertyName">属性名称</param>
        protected void AddPropertyChange(string propertyName)
        {
            if (!NeedRecordPropertyChange)
            {
                return;
            }
            if (!_PropertyChanges.Contains(propertyName))
            {
                _PropertyChanges.Add(propertyName);
            }
        }
        /// <summary>
        /// 清除PK修改项
        /// </summary>
        public void ClearPKChanges()
        {
            if (!NeedRecordPropertyChange)
            {
                return;
            }
            _PKPropertyChanges.Clear();
        }
        /// <summary>
        /// 清除属性修改项
        /// </summary>
        public void ClearPropertyChanges()
        {
            if (!NeedRecordPropertyChange)
            {
                return;
            }
            _PropertyChanges.Clear();
        }
        /// <summary>
        /// 清除所有修改项
        /// </summary>
        public void ClearChanges()
        {
            if (!NeedRecordPropertyChange)
            {
                return;
            }
            _PropertyChanges.Clear();            
        }
    }
}
